'''
Created on May 10, 2011

@author: bruk
'''
import cProfile as profile

import time
import string #@UnresolvedImport
import random #@UnresolvedImport
import Actor
import Camera
# PyGame Constants
import pygame #@UnresolvedImport
from pygame.locals import *
from pygame.color import THECOLORS #@UnresolvedImport
import Settings

class Engine():
    def __init__(self, width=Settings.SCREEN_WIDTH,height=Settings.SCREEN_HEIGHT):
        pygame.init()
        self.Screen = pygame.display.set_mode((width,height),0,8)
        self.Screen.fill(THECOLORS["black"])
        self.Exit = False
        self.Clock = pygame.time.Clock()

        #key events, physics optional
        self.Player = []
        #physics
        self.Actors = []
        #no physics!
        self.GhostActors = []
        
        self.Camera = [Camera.Camera()]
        
    def addActor(self, actor):
        if  actor.Ghost and not  actor.Player:
            self.GhostActors.append(actor)
        elif  actor.Player:
            self.Player.append(actor)
        else:
            self.Actors.append(actor)
            
    def Run(self):
        while not self.Exit:
            self.Clock.tick(Settings.FPS)

            tick = pygame.time.get_ticks()
            self.Screen.fill(THECOLORS["white"])
            activeCam = None
            for cam in self.Camera: 
                if cam.Active: 
                    activeCam = cam
                    break
                
            if activeCam == None:
                print "No active camera!"
                self.Exit = True
                
            activeCam.TrackTo(self.Player[0].Position)
            
            events = pygame.event.get()
                        
            for event in events:
                if (event.type == QUIT):
                    self.Exit = True
                    break
                elif (event.type == KEYDOWN):
                    if (event.key == K_ESCAPE):
                        self.Exit = True
                        break
                    
                    
            for  actor in self.Player:
                actor.Think(events=events)
                #Run collisions!
                
                print "Navi" + str(self.Actors[0].rect)
                print "Link" + str(self.Player[0].rect)
                
                
                #Did we collide?
                actor.MoveX(actor.Velocity[0])
                if pygame.sprite.collide_rect(actor, self.Actors[0]):
                    actor.MoveX(-1 * actor.Velocity[0])
                    print "Collided"

                actor.MoveY(actor.Velocity[1])
                if pygame.sprite.collide_rect(actor, self.Actors[0]):
                    actor.MoveY(-1 *actor.Velocity[1])
                    print "Collided"
                
                
                
                actor.update(tick)
                self.Screen.blit( actor.image, activeCam.TransformToScreen(actor))
                                                  
            for actor in self.Actors:
                actor.Think(player = self.Player[0])
                actor.update(tick)
                self.Screen.blit( actor.image, activeCam.TransformToScreen(actor))
                
            for  actor in self.GhostActors:
                actor.Think(player = self.Player[0])
                actor.update(tick)
                self.Screen.blit( actor.image, activeCam.TransformToScreen(actor))
                
            activeCam.Think()

           
            pygame.display.flip()
     
        pygame.quit()
        
if __name__ == "__main__":
    # Someone is launching this directly
    newEngine = Engine()
    newEngine.addActor(Actor.Player('/home/bruk/Code fu/steampunked/Resources/bluelink.png',30,30, 15, Actor.Link, 320,240) )
    newEngine.addActor(Actor.Navi())
    profile.run('newEngine.Run()')
    
